﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.ViewModels
{
    public sealed class ProjectEditorViewModel : IProjectEditorViewModel
    {
        public ProjectEditorViewModel()
        {
            this.Project = new Project();
        }

        #region IProjectEditorViewModel Members

        private Project project;
        public Project Project
        {
            get { return this.project; }
            set
            {
                this.project = value;
                this.PropertyChanged.Notify(() => this.Project);
            }
        }

        public Func<Action, UIElement> CreateEditCategories
        {
            get { return onCompleted => this.CreateEditCategoriesService.TryInvoke(this.Project, onCompleted); }
        }
        public Func<Action, UIElement> CreateEditMembers
        {
            get { return onCompleted => this.CreateEditMembersService(this.Project, onCompleted); }
        }

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IProjectEditorViewModelService Members

        public Func<Project, Action, UIElement> CreateEditCategoriesService { get; set; }

        public Func<Project, Action, UIElement> CreateEditMembersService { get; set; }

        #endregion

        #region IEditor Members

        public Action Cancel { get; set; }
        public IObservable<Unit> Save { get; set; }
        public IObservable<Unit> Apply { get; set; }
        public Action<Exception> Failure { get; set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        ~ProjectEditorViewModel()
        {
            this.Dispose();
        }
    }
}
